public class InsertionSort {
    // 插入排序主方法
    public void insertionSort(int[] arr) {
        int n = arr.length;
        for (int i = 1; i < n; i++) {
            int key = arr[i];
            int j = i - 1;
            
            // 将大于 key 的元素后移
            while (j >= 0 && arr[j] > key) {
                arr[j + 1] = arr[j];
                j--;
            }
            
            // 插入 key
            arr[j + 1] = key;
        }
    }

    // 测试用例
    public static void main(String[] args) {
        InsertionSort sorter = new InsertionSort();
        
        // 测试数组
        int[] arr = {5, 2, 4, 6, 1, 3};
        
        System.out.println("排序前的数组:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
        
        sorter.insertionSort(arr);
        
        System.out.println("\n排序后的数组:");
        for (int num : arr) {
            System.out.print(num + " ");
        }
    }
}